package cn.itcast.web.servlet;

import cn.itcast.domain.Req;
import cn.itcast.service.ReqService;
import cn.itcast.service.impl.ReqServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/findReqByIdAndYidaServlet")
public class FindReqByIdAndYidaServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1获取参数
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String yid = request.getParameter("yid");
        String uid = request.getParameter("uid");
        System.out.println(yid);
//        System.out.println(id+"id");
//        System.out.println(yid+"yid");
        Map<String, String[]> map = request.getParameterMap();
        Req req1=new Req();
        try {
            BeanUtils.populate(req1,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        //2调用service查询
        ReqService service=new ReqServiceImpl();
        Req req=service.findReqByIdAndYid(id,yid);
        if(req!=null){
            //2存入request域中

            request.setAttribute("id",id);
            request.setAttribute("yid",yid);
            request.setAttribute("error_msg"," has exist");
            //3转发到Reqlist.jsp中
            request.getRequestDispatcher("/Reqadd.jsp").forward(request,response);
        }else{
//            System.out.println(req1);
            request.setAttribute("id",id);
            request.setAttribute("yid",yid);
            request.setAttribute("req",req1);

            request.getRequestDispatcher("/addReqServlet").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
